package com.gary.springcloud.service;

import com.gary.springcloud.entity.CommonResult;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.concurrent.TimeUnit;

/**
 * @author peng_rui
 * @date 2022/4/5
 */
@Slf4j
@Service
@DefaultProperties(defaultFallback = "defaultGlobalFallback")
public class PaymentService {

    public String hystrixOk() {
        log.info(Thread.currentThread().getName() + " ---> 服务调用成功 hystrix ok ...");
        return  "服务调用成功 hystrix ok ...";
    }

//    @HystrixCommand(fallbackMethod = "hystrixTimeoutHandler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
//    }
    @HystrixCommand
    public String hystrixTimeout() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(Thread.currentThread().getName() + " ---> 服务调用成功 hystrix timeout ...");
        return "服务调用成功 hystrix timeout ...";
    }

    public String hystrixTimeoutHandler() {

        log.info(Thread.currentThread().getName() + " ---> 服务调用成功 hystrix timeout ...");
        return "系统繁忙，请稍后再试 hystrix timeout ...";
    }

    public String defaultGlobalFallback() {
        log.info(Thread.currentThread().getName() + " ---> 服务调用成功 默认全局降级处理 ...");
        return "系统繁忙，请稍后再试 defaultGlobalFallback ...";
    }
}
